package com.wpre.lotterysystem.controller;

import com.wpre.lotterysystem.common.pojo.CommonResult;
import com.wpre.lotterysystem.controller.param.DrawPrizeParam;
import com.wpre.lotterysystem.controller.param.ShowWinningRecordsParam;
import com.wpre.lotterysystem.controller.result.ShowWinningRecordsResult;
import com.wpre.lotterysystem.dao.dataobject.WinningRecordDO;
import com.wpre.lotterysystem.service.DrawPrizeService;
import com.wpre.lotterysystem.service.dto.WinningRecordDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-12-22
 * Time: 19:46
 */
@Slf4j
@RestController
public class DrawPrizeController {

    @Autowired
    private DrawPrizeService drawPrizeService;

    //抽奖
    @RequestMapping("draw-prize")
    public CommonResult<Boolean> drawPrize(@RequestBody @Validated DrawPrizeParam param, BindingResult bindingResult) {
        log.info(String.valueOf(param));
        drawPrizeService.drawPrize(param);//将消息发送给消息队列,然后返回
        return CommonResult.success(true);
    }

    @RequestMapping("/winning-records/show")
    public CommonResult<List<ShowWinningRecordsResult>> showWinningRecords(
            @Validated @RequestBody ShowWinningRecordsParam param) {
        List<WinningRecordDTO> winningRecordDTO = drawPrizeService.getRecords(param);
        return CommonResult.success(convertToShowWinningRecordsResult(winningRecordDTO));
    }

    private List<ShowWinningRecordsResult> convertToShowWinningRecordsResult(
            List<WinningRecordDTO> winningRecordDTO) {
        if (winningRecordDTO == null) {
            return Arrays.asList();
        }
        return winningRecordDTO.stream()
                .map(dto -> {
                    ShowWinningRecordsResult result = new ShowWinningRecordsResult();
                    result.setWinnerId(dto.getWinnerId());
                    result.setWinnerName(dto.getWinnerName());
                    result.setPrizeName(dto.getPrizeName());
                    result.setPrizeTier(dto.getPrizeTier().getMessage());
                    result.setWinningTime(dto.getWinningTime());

                    return result;
                }).collect(Collectors.toList());

    }


}
